import urllib2
import urllib
import xml.etree.ElementTree as xmlParser

class Keyword:
	def __init__(self, priority, keyword):
		self.priority = priority
		self.keyword = keyword
	def __unicode__(self):
		return self.keyword

def ask_zemanta(content):
	keywords = []

	data = {}
	data['method'] = "zemanta.suggest"
	data['api_key'] = "apqqsfz7az5s3tr5ynjv3nkk"
	data['text'] = content
	data['format'] = "xml"

	str_data = {}
	for k, v in data.iteritems():
		str_data[k] = unicode(v).encode('utf-8')

	urlValues = urllib.urlencode(str_data)
	url = 'http://api.zemanta.com/services/rest/0.0/'
	headers = { 'method' : 'POST',
		'Content-type': 'application/x-www-form-urlencoded',
		'Content-length' : len(urlValues),
		'content' : urlValues}
	req = urllib2.Request(url, urlValues, headers)
	response = urllib2.urlopen(req)
	the_page = response.read()
	dom = xmlParser.fromstring(the_page).find("keywords").findall("keyword");
	i = 1
	for e in dom:
		keywordText = e.find("name").text
		keyword = Keyword(i,keywordText)
		keywords.append(keyword)
		i+=1
	return keywords
